package com.xwj.TimeRequest;

import com.xwj.entity.RaHourseInfo;
import com.xwj.service.Impl.RaHouseInfoServiceImpl;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 定时任务
 */

@Component
public class RaHouseInfoTimeQuestImpl implements TimeRequestTask {

    @Autowired
    private RaHouseInfoServiceImpl raHouseInfoService;

    /**
     * 每分钟检查一次
     */
    @Override
    @Scheduled(cron = "0 0/1 * * * ?")
    public void preCheck() {
        List<RaHourseInfo> raHourseInfoList = raHouseInfoService.findAllHouseInfo();
        List<String> ids = new ArrayList<>();
        raHourseInfoList.forEach(raHourseInfo -> {
            long expireTime = DateTime.parse(raHourseInfo.getExpireTime(), DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).getMillis();
            if (System.currentTimeMillis() >= expireTime)
                ids.add(raHourseInfo.getId());
        });
        if (ids.size() > 0)
            raHouseInfoService.updateDelflag(ids);
    }
}


